package com.ruyuan.dependency.product.controller;

import com.ruyuan.dependency.product.entity.Brand;
import com.ruyuan.dependency.product.entity.Category;
import com.ruyuan.dependency.product.entity.Sku;
import com.ruyuan.dependency.product.entity.Spu;
import com.ruyuan.dependency.product.service.BrandService;
import com.ruyuan.dependency.product.service.CategoryService;
import com.ruyuan.dependency.product.service.SkuService;
import com.ruyuan.dependency.product.service.SpuService;
import com.ruyuan.dependency.product.vo.SkuIdAndStock;
import com.ruyuan.dependency.product.vo.SkuVo;
import com.ruyuan.dependency.product.vo.SpuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhonghuashishan
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController{

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/queryBySkuId")
    public SkuVo queryBySkuId(@RequestParam("skuId") Long skuId) {
        Sku sku = skuService.queryById(skuId);
        SkuVo skuVo = new SkuVo();
        BeanUtils.copyProperties(sku, skuVo);
        return skuVo;
    }

    @PostMapping("/queryBySkuIds")
    public List<SkuVo> queryBySkuIds(@RequestBody List<Long> skuIds) {
        List<Sku> skus = skuService.queryByIds(skuIds);
        List<SkuVo> skuVos = new ArrayList<>();
        for (Sku sku : skus) {
            SkuVo skuVo = new SkuVo();
            BeanUtils.copyProperties(sku, skuVo);
            skuVos.add(skuVo);
        }
        return skuVos;
    }
    @GetMapping("/queryBySpuId")
    public SpuVo queryBySpuId(@RequestParam("spuId") Long spuId) {
        Spu spu = spuService.queryById(spuId);
        Brand brand = brandService.queryById(spu.getBrandId());
        Category category = categoryService.queryById(spu.getCategoryId());

        return SpuVo.builder()
                .id(spu.getId())
                .name(spu.getName())
                .brandId(brand.getId())
                .brandName(brand.getName())
                .brandLogo(brand.getLogo())
                .categoryId(category.getId())
                .categoryName(category.getName())
                .build();
    }

    @PutMapping("/lockStock")
    public Boolean lockStock(@RequestParam("skuId") Long skuId,
                             @RequestParam("count") Integer count) {
        if (skuService.lockStock(skuId, count)) {
            log.info("锁定商品库存, skuId={}, 锁定的库存={}", skuId, count);
            return Boolean.TRUE;
        }
        log.info("锁定商品库存但是库存不足, skuId={}, 锁定的库存={}", skuId, count);
        return Boolean.FALSE;
    }

    @PutMapping("/releaseStock")
    public Boolean releaseStock(@RequestParam("skuId") Long skuId,
                                @RequestParam("count") Integer count) {
        skuService.releaseStock(skuId, count);
        log.info("释放商品库存, skuId={}, 锁定的库存={}", skuId, count);
        return Boolean.TRUE;
    }

    @PutMapping("/batchReleaseStock")
    public Boolean batchReleaseStock(@RequestBody List<SkuIdAndStock> skuIdAndStockList) {
        for (SkuIdAndStock skuIdAndStock : skuIdAndStockList) {
            skuService.releaseStock(skuIdAndStock.getSkuId(), skuIdAndStock.getStock());
            log.info("释放商品库存, skuId={}, 锁定的库存={}", skuIdAndStock.getSkuId(), skuIdAndStock.getStock());
        }
        return Boolean.TRUE;
    }
}
